var app = getApp()
var gb = app.globalData
var Bmob = gb.Bmob
var SessionMgr = gb.SessionMgr
var BmobObj = gb.BmobObj

var _statusCode = 0           //点击过来的code
var _orderObjectId = ""       //订单id
var _crtOrder = null          //订单

var _images = []            //加载出来的图片列表

Page({
  data:{
    images:null,
    showUtil:false            //是否显示按钮
  },
  onLoad:function(options){

    var self = this
    
    _statusCode = options.statusCode
    _orderObjectId = options.orderObjectId

    var Order = Bmob.Object.extend("order")
    var query = new Bmob.Query(Order)
    query.get(_orderObjectId,{
      success:function(result){
        _crtOrder = BmobObj.func.orderList([result])[0]

        if(_crtOrder.status_code == 2 ){
          //订单已拍片
            if(_statusCode == 2){
              //查看已拍片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(2)
            }
            else if (_statusCode == 3){
              //客户选粗片
              self.setData({
                showUtil:true
              })
              self.reqLoadOrderStatus(2)
            }
        }
        else if(_crtOrder.status_code == 3 ){
            //客人已选片
            if(_statusCode == 2){
              //查看已拍片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(2)
            }
            else if (_statusCode == 3){
              //客人已选片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(3)
            }
        }
        else if(_crtOrder.status_code == 4 ){
            //已修片
            if(_statusCode == 2){
              //查看已拍片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(2)
            }
            else if (_statusCode == 3){
              //客人已选片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(3)
            }
            else if (_statusCode == 4){
              //客人查看修片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(4)
            }

            else if (_statusCode == 5){
              //客人选精修片
              self.setData({
                showUtil:true
              })
              self.reqLoadOrderStatus(4)
            }
        }

        else if(_crtOrder.status_code == 5 ){
            //客人已选精片

            if(_statusCode == 2){
              //查看已拍片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(2)
            }
            else if (_statusCode == 3){
              //客人已选片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(3)
            }
            else if (_statusCode == 4){
              //客人查看修片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(4)
            }
            
            if(_statusCode == 5){
                //客人查看自己已经选了的精片
                self.reqLoadOrderStatus(5)
            }

            if(_statusCode == 6){
              //客人挑选模板
              self.setData({
                showUtil:true
              })
              self.reqLoadTemplate()
            }
        }

        else if(_crtOrder.status_code == 6 ){
            //客人已选模板

            if(_statusCode == 2){
              //查看已拍片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(2)
            }
            else if (_statusCode == 3){
              //客人已选片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(3)
            }
            else if (_statusCode == 4){
              //客人查看修片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(4)
            }
            
            if(_statusCode == 5){
                //客人查看自己已经选了的精片
                self.reqLoadOrderStatus(5)
            }

            if(_statusCode == 6){
              //客人查看已选模板
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(6)
            }
        }
        else if(_crtOrder.status_code == 7 || _crtOrder.status_code == 8 ){
            //已出成片

            if(_statusCode == 2){
              //查看已拍片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(2)
            }
            else if (_statusCode == 3){
              //客人已选片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(3)
            }
            else if (_statusCode == 4){
              //客人查看修片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(4)
            }
            
            if(_statusCode == 5){
                //客人查看自己已经选了的精片
                self.reqLoadOrderStatus(5)
            }

            if(_statusCode == 6){
              //客人查看已选模板
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(6)
            }
            else if (_statusCode == 7){
              //查看成片
              self.setData({
                showUtil:false
              })
              self.reqLoadOrderStatus(7)
            }
        }
      }
    })

  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  onSubmit:function(e){
    //提交按钮
    var imgsStr = ""
    for(var index = 0 ; index < _images.length; index++){
        var item = _images[index]
        if(item.selected){
          if(imgsStr.length == 0){
            imgsStr = item.url
          }else{
            imgsStr = imgsStr + "[/]" + item.url
          }
        }
    }

    if(imgsStr.length == 0){
      wx.showModal({
        title: '提示',
        content: '请选择照片',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      })
      return
    }

    var _saveStatusCode = 0           //待新增order_status的code
    if(_crtOrder.status_code == 2 ){
      //订单已拍片
        if (_statusCode == 3){
          //客户选粗片
          _saveStatusCode = 3
        }
    }

    else if(_crtOrder.status_code == 4){
      //订单已修片
        if(_statusCode == 5){
            //客人选精修片
            _saveStatusCode = 5
        }
    }
    else if(_crtOrder.status_code == 5 ){
      //客人已选精片
      if(_statusCode == 6){
        //客人挑选模板
        _saveStatusCode = 6;
      }
    }

    var order = Bmob.Object.createWithoutData("order",_orderObjectId)
      order.increment("status_code")
      order.save(null,{
        success:function(obj){

          SessionMgr.getSessionUser({
            success:function(userinfo){
                var OStatus = Bmob.Object.extend("order_status")
                var os = new OStatus()
                os.set("status_code",_saveStatusCode)
                os.set("user_object_id",userinfo.objectId)
                os.set("user_name",userinfo.nickname)
                os.set("order_object_id",_orderObjectId)
                os.set("images",imgsStr)
                os.set("business_identify",SessionMgr.getConstant().bIdentify)

                os.save(null,{
                  success:function(obj){

                    wx.showToast({
                      title: '成功',
                      icon: 'success',
                      duration: 2000
                    })
                    setTimeout(function(){
                      wx.navigateBack({
                          delta: 1
                        })
                    },2000)
                  }
                })
            }
          })

        }
      })

  },
  onSelectedSwitch:function(e){
    var index = e.target.dataset.index
    var data = _images[index]
    data.selected = !data.selected
    _images[index] = data

    this.setData({
        images:_images
    })
  },
  reqLoadTemplate:function(){
      //加载该商户的模板
      var self = this

      var BTmp = Bmob.Object.extend("business_template")
      var query = new Bmob.Query(BTmp)
      query.equalTo("business_identify",SessionMgr.getConstant().bIdentify)
      query.find({
          success:function(results){
              var obj = BmobObj.func.businessTemplateList(results)[0]

              _images = []
              var imgs = obj.images.split("[/]")
              for(var index = 0 ; index < imgs.length; index++ ){
                  _images.push({
                    index:index,
                    url:imgs[index],
                    selected:false
                  })
              }

              self.setData({
                images:_images
              })
          }
      })
  },
  reqLoadOrderStatus:function(statusCode){
    //查看已拍片
      console.log("准备查询OrderStatus.code="+statusCode)

      var self = this

      var OrderStatus = Bmob.Object.extend("order_status")
      var query = new Bmob.Query(OrderStatus)

      query.equalTo("status_code",statusCode)
      query.equalTo("order_object_id",_orderObjectId)

      query.find({
        success:function(results){
            var obj = BmobObj.func.orderStatusList(results)[0]

            console.log(obj)

            var images = obj.images.split("[/]")
            
            _images = []
            for(var index = 0 ; index < images.length; index++){
                var data = {}
                var img = images[index]
                data.url = img
                data.index = index
                data.selected = false
                _images.push(data)
            }

            self.setData({
              images:_images
            })
        },
        error:function(err){
          console.log(err)
        }
      })
  }
})